package com.vectz.autogen.action;

import java.io.IOException;
import java.util.ArrayList;

import com.vectz.autogen.model.Entity;
import com.vectz.autogen.model.EntityOverview;
import com.vectz.autogen.util.Constant;
import com.vectz.autogen.util.Log;
import com.vectz.autogen.util.Util;

/**
 * @author ai-t.vn
 * 
 */
public class JsfListCodegenAction {
	ArrayList<Entity> listField=null;
	EntityOverview table = null;
	String outputFolder = "";
	public JsfListCodegenAction(ArrayList<Entity> listField, EntityOverview table, String outputFolder) {
		this.listField = listField;
		this.table = table;
		this.outputFolder = outputFolder;
		genernateJsfFile();
	}

	/*
	 * Generate jsf file
	 */
	public void genernateJsfFile() {
		try {
			String tmplFormContent = Util.readTextFileContent(Util
					.getTmplPath("jsf", "Form-ListEntity.txt"));
			String tmplFieldContent = Util.readTextFileContent(Util
					.getTmplPath("jsf", "ListEntityData.txt"));

			String tmplFieldHeader = Util.readTextFileContent(Util
					.getTmplPath("jsf", "ListEntityHeader.txt"));

			
			tmplFormContent = tmplFormContent.replaceAll("xxxxFormId",
					table.getScreenId());
			
			tmplFormContent = tmplFormContent.replaceAll("xxxxInitNew",Util.buildButtonAction(table.getName(), "initNew"));
			tmplFormContent = tmplFormContent.replaceAll("xxxxInitModify",Util.buildButtonAction(table.getName(), "initModify"));
			tmplFormContent = tmplFormContent.replaceAll("xxxxEntityId",getIdField(listField));
			tmplFormContent = tmplFormContent.replaceAll("xxxxItem",Util.lowerCaseFirstChar(table.getName()));
			//initial list column width
			if(listField!=null && listField.size()!=0){
				//first column width
				String arrayWidth="4%";
				//calculate column width for another column
				int widthAverage = 96/listField.size();
				for (int i = 0; i < listField.size(); i++) {
					arrayWidth += new StringBuffer().append(",")
					.append(widthAverage)
					.append("%");
					
				}
				tmplFormContent = tmplFormContent.replaceAll("xxxxArrayWidth",arrayWidth);
			}

			//replace column header
			String listHeaderColumn = genernateHeaderColumn(listField,tmplFieldHeader );
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxColumnHeader", listHeaderColumn);
			
			// add column data
			String listFieldForm = genernateColumn(listField,
					tmplFieldContent);
			tmplFormContent = tmplFormContent.replaceAll(
					"xxxxColumnData", listFieldForm);

			String fileLocation = new StringBuffer().append(outputFolder)
			.append(System.getProperty("file.separator"))
			.append("WebContent")
			.append(System.getProperty("file.separator"))
			.append(Util.getWebbaseDir(table.getDestScreenPath()))
			.append(System.getProperty("file.separator"))
			.append("list")
			.append(Util.lowerCaseFirstChar(table.getName()))
			.append(".xhtml").toString();
			
			Util.writeJavaFile(fileLocation,tmplFormContent);
			Log.log("Finish generate jsf file to:"+ fileLocation);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/*
	 * Generate header name of column
	 */
	public String genernateHeaderColumn(ArrayList<Entity> listField, String tmplContent) {
		StringBuffer content = new StringBuffer();
		if (listField == null && listField.size() == 0)
			return "";
		Entity field = null;
		for (int i = 0; i < listField.size(); i++) {
			field = (Entity) listField.get(i);
			//check only display=show then add new component
			if (field.getDisplay().equalsIgnoreCase("show")) {
				String tmp = tmplContent;
				tmp = tmp.replaceAll("xxxxHeader",field.getLogicalName());
				content.append(tmp);
			}
		}

		return content.toString();
	}
	
	/*
	 * Generate header value of column
	 */
	public String genernateColumn(ArrayList<Entity> listField,
			String tmplContent) {
		StringBuffer content = new StringBuffer();
		if (listField == null && listField.size() == 0)
			return "";
		Entity field = null;
		for (int i = 0; i < listField.size(); i++) {
			field = (Entity) listField.get(i);
			//check only display=show then add new component
			if (field.getDisplay().equalsIgnoreCase("show")) {
				String tmp = tmplContent;
				tmp = tmp.replaceAll("xxxxOuputValue",getInputValue(field));
				content.append(tmp);
			}
		}

		return content.toString();
	}
	
	/*
	 * Generate Id field in model class
	 */
	public String getIdField(ArrayList<Entity> listField){
		if (listField == null && listField.size() == 0)
			return "";
		Entity field = null;
		for (int i = 0; i < listField.size(); i++) {
			field = (Entity) listField.get(i);
			if(field.getKeyType().equalsIgnoreCase("PK")){
				return new StringBuffer()
				.append(table.getPackageName())
				.append(".")
				.append(Util.lowerCaseFirstChar(table.getName()))
				.append(".")
				.append(field.getName()).toString();
			}
		}
		return "";
	}

	/**
	 * Get input value with pattern: #{entityAction.entity.field}
	 * 
	 * @param entity
	 * @return input value
	 */
	public String getInputValue(Entity entity) {
		return new StringBuffer().append("#{").append(
				Util.lowerCaseFirstChar(table.getName())).append("Action.")
				.append(Util.lowerCaseFirstChar(table.getName())).append(".")
				.append(entity.getName()).append("}").toString();
	}
}
